define(function(require, exports){
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var util = require('util');
	var $ = require('jquery');
	var dspUtil = require('dspUtil');

	var Device = view.container.extend({
		init: function( config ) {
			config = pubjs.conf( 0, config, {
				'Type': 'device',
				'TypeName': LANG('设备型号'),
				'TypeId': 'deviceModel',
				'data': null,
				'key': 'children',
				'valueKey': 'BdxId',
				'nameKey': 'Name'
				// data: [dspUtil._convertArrayGroup(LANG('设备型号'), 'deviceModel', pubjs.config("mobileClient/deviceType"))]
			});
			this.Super( 'init', arguments );
		}
		,afterBuild: function() {
			var c = this.getConfig();
			this.$Type = c.Type;

			this.append([
				'<div pub-name="'+this.$Type+'" pub-mod="@base/common/input.subLevelCheckbox"/>'
			].join(''));

			var opt = {};
			opt[this.$Type] = {
				'class': 'M-commonSLC M-deviceType',
				'key': c.key,
				'valueKey': c.valueKey,
				'nameKey': c.nameKey
			}

			this.buildFromTemplate(opt);

		}
		,afterBuildTemplate: function() {

			var c = this.getConfig();
			if(c.data){
				this.$allData = c.data;
				this.setList([{name: LANG('全选'), child: c.data}]);
			}else{
				this.load();
			}
		}
		,setList: function(data){
			if(this.$[this.$Type]){
				this.$[this.$Type].setList(data);
				// 整理数据
				// var data = this.getConfig('data');
				// this.$[this.$Type].setList([{name: LANG('全选'), child: data}]);
				// 反选按钮与清空按钮
				var buttonsDiv = $('<div class="M-commonSLCButtons"></div>');
				//$('<a class="selectInverse">反选</a>').appendTo(buttonsDiv);
				this.dg(buttonsDiv, '.selectInverse', 'click', 'eventSelectInverse');
				$('<a class="clearAll">'+LANG('清空')+'</a>').appendTo(buttonsDiv);
				this.dg(buttonsDiv, '.clearAll', 'click', 'eventClearAll');
				this.$[this.$Type].$el.prepend(buttonsDiv);
				this.$el.find('.M-commonSLCZoneHead').hide();
				this.$el.find('.M-commonSLCZoneBody').css('margin-left',0);
				this.$[this.$Type].eventHoverItem = this.eventHoverItem;
			}
		}
		,load: function(){
			var self = this;
			var c = self.getConfig();
			pubjs.mc.send(
				c.url,
				$.extend({}, self.$param, c.param),
				self.onData.bind(self)
			);
		}
		,onData: function(err, data){
			if (err) {
				pubjs.error(err);
				return false;
			}
			var c = this.getConfig();

			this.$allData = data;

			data = [{
				'Name': LANG('全部'),
				'children': [{
					'Name': c.TypeName,
					'BdxId': c.TypeId,
					'children': data
				}]
			}];


			this.setList(data);
		}
		,eventHoverItem: function(){
			// 重置鼠标回调事件
		}
		,eventSelectInverse: function() {
			var c = this.getConfig();
			// 定义反选的数据data
			var inverseData = {};

			// 构建data的内容
			var inverseOsArray = this._getInverseArray(c.TypeId);
			if (inverseOsArray.length > 0) {
				inverseData[c.TypeId] = inverseOsArray;
			}

			this.setData(inverseData);
		}
		,_getInverseArray: function(name) {
		}
		,eventClearAll: function() {
			this.$[this.$Type].setData([]);
		}
		,getValue: function() {
			var ret = this.$[this.$Type].getData();
			var data={}, all={};
			util.each(ret, function(id)
			{
				var val = id.split(':');
				var type = val.shift();
				if (val.length === 0)
				{
					if (type)
					{
						all[type] = true;
					}
				}
				else
				{
					if (val.length === 1)
					{
						val = isNaN(+val[0]) ? val[0] : +val[0];
					}
					else
					{
						val = val.join(':');
					}

					if (!data[type])
					{
						data[type] = [val];
					}
					else
					{
						data[type].push(val);
					}
				}
			});

			util.each(data, function(list, type)
			{
				if (all[type])
				{
					return [];
				}
			});

			return data.deviceModel;
		}
		,setValue: function(data) {
			var sels = [];
			data = {deviceModel: data};
			util.each(data, function(list, type)
			{
				util.each(list, function(id)
				{
					sels.push(type + ':' + id);
				});
			});

			this.$[this.$Type].setData(sels);
			return this;
		}
		,setData: function(data){
			this.$[this.$Type].setData(data);
			return this;
		}
		,getDesc: function(data){
			var self = this;
			var el = self.getDOM();
			var result = [];
			if(data){
				util.each(data, function(item){
					var name = el.find('input[value="'+item+'"]').next().text();
					result.push(name);
				});
			}
			return result;
		}
	});

	exports.base = Device;
});